package com.tt.lcd.qunyangGPRS.activity;

import java.util.ArrayList;
import java.util.List;

import cn.androiddevelop.cycleviewpager.lib.CycleViewPager;
import cn.androiddevelop.cycleviewpager.lib.CycleViewPager.ImageCycleViewListener;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.tt.lcd.qunyangGPRS.R;
import com.tt.lcd.qunyangGPRS.ZSactivity.BuYu;
import com.tt.lcd.qunyangGPRS.ZSactivity.DaiQiing;
import com.tt.lcd.qunyangGPRS.ZSactivity.Geshita;
import com.tt.lcd.qunyangGPRS.ZSactivity.Peizhong;
import com.tt.lcd.qunyangGPRS.ZSactivity.XunLian;
import com.tt.lcd.qunyangGPRS.aSDK.SDKserver;
import com.tt.lcd.qunyangGPRS.bean.ADInfo;
import com.tt.lcd.qunyangGPRS.dao.MyUser;
import com.tt.lcd.qunyangGPRS.systemActivity.UserActivity;
import com.tt.lcd.qunyangGPRS.utils.MyApplication;
import com.tt.lcd.qunyangGPRS.utils.Register;
import com.tt.lcd.qunyangGPRS.utils.ViewFactory;
import com.tt.lcd.qunyangGPRS.ZSactivity.XiaoQunYang;
import com.tt.lcd.qunyangGPRS.activity.Main_activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class Main_activity extends Activity {
	private ImageView bt_renshen, temp, bt_chanfang, daiqing, baoyu, xunlian;

	private SDKserver server;

	private ServiceConnection scon = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName name) {
			// server 不正常结束时回调
			Log.e("", "服务非正常结束");
		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// 回调函数
			Log.e("", "onServiceConnected");
			server = ((SDKserver.LocalBinder) service).getServeice();
			Register.getRegister().server = server;

		}
	};

	// ~~~~~~~~~~~轮播图片相关的参数
	private List<ImageView> views = new ArrayList<ImageView>();
	private List<ADInfo> infos = new ArrayList<ADInfo>();
	private CycleViewPager cycleViewPager;

	private String[] imageUrls = { "drawable://" + R.drawable.xiaoqunyang,
			"drawable://" + R.drawable.geshita, "drawable://" + R.drawable.cc1, };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// 如果没注册跳转到注册界面
		if (new MyUser(this).selectUID().equals("")) {
			Intent it = new Intent();
			it.setClass(Main_activity.this, UserActivity.class);
			startActivity(it);
		}
		
		// 启动ASDK服务
		Intent it = new Intent(MyApplication.getContext(), SDKserver.class);
		bindService(it, scon, BIND_AUTO_CREATE);

		
		
		
		
		
		// 轮播图片初始化
		configImageLoader();
		initialize();

		bt_chanfang = (ImageView) findViewById(R.id.bt_chanfang);
		bt_chanfang.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent it = new Intent();
//				it.setClass(Main_activity.this, Chanfang_Info.class);
				it.setClass(Main_activity.this, Geshita.class);
				startActivity(it);

			}
		});

		bt_chanfang.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				LinearLayout ll = (LinearLayout) bt_chanfang.getParent();
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					// 更改为按下时的背景图�?
					ll.setBackgroundColor(Color.rgb(238, 238, 238));
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					// 改为抬起时的图片
					ll.setBackgroundColor(Color.rgb(255, 255, 255));
				}
				return false;
			}
		});

		daiqing = (ImageView) findViewById(R.id.daiqing);
		daiqing.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				Log.e("mainActivity", "发送");
				Intent it = new Intent();
				it.setClass(Main_activity.this, DaiQiing.class);
				startActivity(it);

			}
		});

		daiqing.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				LinearLayout ll = (LinearLayout) daiqing.getParent();
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					// 更改为按下时的背景图�?
					ll.setBackgroundColor(Color.rgb(238, 238, 238));
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					// 改为抬起时的图片
					ll.setBackgroundColor(Color.rgb(255, 255, 255));
				}
				return false;
			}
		});

		baoyu = (ImageView) findViewById(R.id.baoyu);
		baoyu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent it = new Intent();
				it.setClass(Main_activity.this, BuYu.class);
				startActivity(it);

			}
		});

		baoyu.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				LinearLayout ll = (LinearLayout) baoyu.getParent();
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					// 更改为按下时的背景图�?
					ll.setBackgroundColor(Color.rgb(238, 238, 238));
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					// 改为抬起时的图片
					ll.setBackgroundColor(Color.rgb(255, 255, 255));
				}
				return false;
			}
		});

		xunlian = (ImageView) findViewById(R.id.xunlian);
		xunlian.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent it = new Intent();
				it.setClass(Main_activity.this, XunLian.class);
				startActivity(it);

			}
		});

		xunlian.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				LinearLayout ll = (LinearLayout) xunlian.getParent();
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					// 更改为按下时的背景图�?
					ll.setBackgroundColor(Color.rgb(238, 238, 238));
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					// 改为抬起时的图片
					ll.setBackgroundColor(Color.rgb(255, 255, 255));
				}
				return false;
			}
		});

		temp = (ImageView) findViewById(R.id.temp);
		temp.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent it = new Intent();
				it.setClass(Main_activity.this, Peizhong.class);
				startActivity(it);

			}
		});
		temp.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				LinearLayout ll = (LinearLayout) temp.getParent();
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					// 更改为按下时的背景图�?
					ll.setBackgroundColor(Color.rgb(238, 238, 238));
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					// 改为抬起时的图片
					ll.setBackgroundColor(Color.rgb(255, 255, 255));
				}
				return false;
			}
		});

		bt_renshen = (ImageView) findViewById(R.id.bt_renshen);
		bt_renshen.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent it = new Intent();
				it.setClass(Main_activity.this, Qunyang_Info.class);
				startActivity(it);
			}
		});

		bt_renshen.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				LinearLayout ll = (LinearLayout) bt_renshen.getParent();
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					// 更改为按下时的背景图片
					ll.setBackgroundColor(Color.rgb(238, 238, 238));
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					// 改为抬起时的图片
					ll.setBackgroundColor(Color.rgb(255, 255, 255));
				}
				return false;
			}
		});

	}

	@Override
	protected void onDestroy() {
		unbindService(scon);
		super.onDestroy();
	}

	// 轮播图片相关的方法

	@SuppressLint("NewApi")
	private void initialize() {

		cycleViewPager = (CycleViewPager) getFragmentManager()
				.findFragmentById(R.id.fragment_cycle_viewpager_content);

		for (int i = 0; i < imageUrls.length; i++) {
			ADInfo info = new ADInfo();
			info.setUrl(imageUrls[i]);
			info.setContent("图片-->" + i);
			infos.add(info);
		}

		// 将最后一个ImageView添加进来
		views.add(ViewFactory.getImageView(this, infos.get(infos.size() - 1)
				.getUrl()));
		for (int i = 0; i < infos.size(); i++) {
			views.add(ViewFactory.getImageView(this, infos.get(i).getUrl()));
		}
		// 将第�?个ImageView添加进来
		views.add(ViewFactory.getImageView(this, infos.get(0).getUrl()));

		// 设置循环，在调用setData方法前调�?
		cycleViewPager.setCycle(true);

		// 在加载数据前设置是否循环
		cycleViewPager.setData(views, infos, mAdCycleViewListener);
		// 设置轮播
		cycleViewPager.setWheel(true);

		// 设置轮播时间，默�?5000ms
		cycleViewPager.setTime(5000);
		// 设置圆点指示图标组居中显示，默认靠右
		cycleViewPager.setIndicatorCenter();
	}

	private ImageCycleViewListener mAdCycleViewListener = new ImageCycleViewListener() {

		@Override
		public void onImageClick(ADInfo info, int position, View imageView) {
			if (cycleViewPager.isCycle()) {
				position = position - 1;
				// Toast.makeText(Main_activity.this,
				// "position-->" + info.getContent(), Toast.LENGTH_SHORT)
				// .show();
				if (info.getContent().equals("图片-->0")) {
					Intent it = new Intent();
					it.setClass(Main_activity.this, XiaoQunYang.class);
					startActivity(it);
				} else if (info.getContent().equals("图片-->1")) {
					Intent it = new Intent();
					it.setClass(Main_activity.this, Geshita.class);
					startActivity(it);
				} else if (info.getContent().equals("图片-->2")) {
					Intent it = new Intent();
					it.setClass(Main_activity.this, BuYu.class);
					startActivity(it);
				}
			}

		}

	};

	/**
	 * 配置ImageLoder
	 */
	private void configImageLoader() {
		// 初始化ImageLoader
		@SuppressWarnings("deprecation")
		DisplayImageOptions options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.icon_stub) // 设置图片下载期间显示的图�?
				.showImageForEmptyUri(R.drawable.icon_empty) // 设置图片Uri为空或是错误的时候显示的图片
				.showImageOnFail(R.drawable.icon_error) // 设置图片加载或解码过程中发生错误显示的图�?
				.cacheInMemory(true) // 设置下载的图片是否缓存在内存�?
				.cacheOnDisc(true) // 设置下载的图片是否缓存在SD卡中
				// .displayer(new RoundedBitmapDisplayer(20)) // 设置成圆角图�?
				.build(); // 创建配置过得DisplayImageOption对象

		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				getApplicationContext()).defaultDisplayImageOptions(options)
				.threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.discCacheFileNameGenerator(new Md5FileNameGenerator())
				.tasksProcessingOrder(QueueProcessingType.LIFO).build();
		ImageLoader.getInstance().init(config);
	}

}
